<?php
require_once('../config/config.php');
require_once('../config/error_handler.php');

// Chat class that contains server-side profiel
class Profiel {

    // database handler
    private $mMysqli;

    // constructor opens database connection
    function __construct() {
        // connect to the database
        $this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    }

    // destructor closes database connection
    public function __destruct() {
        $this->mMysqli->close();
    }

    //The postMessages method inserts a profile into the database
    public function postMessage($instellingNaam, $contactpersoon, $adres, $postcode, 
            $woonplaats, $provincie, $telefoon, $domein, $email, $wachtwoord, $pakket)
    {
        // escape the variable data for safely adding them to the database
        $instellingNaam = $this->mMysqli->real_escape_string($instellingNaam);
        $contactpersoon = $this->mMysqli->real_escape_string($contactpersoon);
        $adres = $this->mMysqli->real_escape_string($adres);
        $postcode = $this->mMysqli->real_escape_string($postcode);
        $woonplaats = $this->mMysqli->real_escape_string($woonplaats);
        $provincie = $this->mMysqli->real_escape_string($provincie);
        $telefoon = $this->mMysqli->real_escape_string($telefoon);
        $domein = $this->mMysqli->real_escape_string($domein);
        $email = $this->mMysqli->real_escape_string($email);
        $wachtwoord = $this->mMysqli->real_escape_string($wachtwoord);
        $pakket = $this->mMysqli->real_escape_string($pakket);

        $querycheck = "SELECT * FROM onderwijsinstelling WHERE email = '".$email."';";
        $querycheck2 = "SELECT * FROM werkgeverprofiel WHERE email = '".$email."';";
        $querycheck3 = "SELECT * FROM studentprofiel WHERE email = '".$email."';";

        // search for existing domein
        $querycheck4 = "SELECT domein_id FROM emaildomein WHERE domein = '" . $domein . "';";
        
        $resultcheck = $this->mMysqli->query($querycheck);
        $resultcheck2 = $this->mMysqli->query($querycheck2);
        $resultcheck3 = $this->mMysqli->query($querycheck3);
        $resultcheck4 = $this->mMysqli->query($querycheck4);
        
        $domeinExists;
        
        // Get the correct domein ID to insert in onderwijsinstelling
        if (mysqli_num_rows($resultcheck4) == 1) {
            $row = mysqli_fetch_array($resultcheck4);
            $domeinExists = $row['domein_id'];
        }
        else {
            echo 'There was a problem finding the correct domain.';
        }
        
        // TODO: Domein toevoegen indien dit door admin is goedgekeurd.
        // Kan momenteel nog niet (afgedwongen in selectievak dat er beperkte keuzes zijn)
        
        
        if (mysqli_num_rows($resultcheck) == 0 && mysqli_num_rows($resultcheck2) == 0 && mysqli_num_rows($resultcheck) == 0 ) {
            
            // set up the query to be executed
            $query = 'INSERT INTO onderwijsinstelling (instellingNaam, contactpersoon, 
                        adres, postcode, woonplaats, provincie, telefoon, domein_id, email, 
                        wachtwoord, pakket ) ' .
                    'VALUES ("' . $instellingNaam . '","' . $contactpersoon . '",
                        "' . $adres . '","' . $postcode . '","' . $woonplaats . '",
                        "' . $provincie . '","' . $telefoon . '","' . $domeinExists .'",
                            "' . $email . '", SHA("' . $wachtwoord . '"),"' . $pakket . '")';

            
            // execute the SQL query
            $result = $this->mMysqli->query($query);

            $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?p=registratiegelukt';
            header('Location: ' . $home_url);
        } else {
            // An account already exists for this username
            $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?p=existaccount';
            header('Location: ' . $home_url);
        }
    }

}
?>
